package DAO.MysqlDAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import DAO.InterfaceDAO.AdministrateurDAO;
import Model.Administrateur;
import Model.Professeur;

public class AdministrateurDAO_Mysql implements AdministrateurDAO{

	private PreparedStatement psCreate, psRead, psUpdate, psDelete, psFind, psExistLogin, psExistNom, psInvitProf, psInvitAdmin;

	public AdministrateurDAO_Mysql(Connection cx) {

		try {
			psCreate = cx.prepareStatement("INSERT INTO administrateur (login, password, nom, prenom,  num_admin, active) VALUES( ?, ?, ?, ?, ?, ?)");
			psRead   = cx.prepareStatement("SELECT * FROM administrateur WHERE id = ?");
			psUpdate = cx.prepareStatement("UPDATE administrateur SET login = ?," +
					" password = ?," +
					" nom = ?," +
					" prenom = ?," +
					" num_admin = ?," +
			" active = ? WHERE id = ? ");
			psDelete = cx.prepareStatement("DELETE FROM administrateur WHERE id = ?");
			psFind   = cx.prepareStatement("SELECT * FROM administrateur");
			psExistLogin  = cx.prepareStatement("SELECT COUNT(*) as exist FROM administrateur WHERE login = ? limit 1");
			psExistNom  = cx.prepareStatement("SELECT COUNT(*) as exist FROM administrateur WHERE nom = ? AND prenom = ? limit 1");
			psInvitProf = cx.prepareStatement("INSERT INTO professeur (nom, prenom, num_prof) VALUES (?, ?, ?)");
			psInvitAdmin = cx.prepareStatement("INSERT INTO administrateur (nom, prenom, num_admin) VALUES (?, ?, ?)");
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public boolean create(Administrateur a) {
		boolean rs=false;
		try {
			psCreate.setString(1, a.getLogin());
			psCreate.setString (2, a.getMotDePasse());
			psCreate.setInt(3, 1);
			psCreate.executeUpdate ();
			rs = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return rs;
	}

	@Override
	public Administrateur read(int id) {
		try {
			psRead.setInt(1, id);
			ResultSet rs = psRead.executeQuery ();
			rs.next();
			int idCompte  = rs.getInt("id");
			String login = rs.getString ("login");
			String motDePasse = rs.getString( "password");
			String nom = rs.getString("nom");
			String prenom = rs.getString("prenom");
			return new Administrateur(idCompte, login, motDePasse, nom, prenom);
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return null;
	}

	@Override
	public boolean update(Administrateur a) {
		boolean rs=false;
		try {
			psUpdate.setString(1, a.getLogin());
			psUpdate.setString (2, a.getMotDePasse());
			psUpdate.setInt(3, 1);
			psUpdate.setInt(3, a.getIdCompte());
			psUpdate.executeUpdate ();
			rs = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return rs;
	}

	@Override
	public boolean delete(Administrateur a) {
		boolean rs=false;
		try {
			psDelete.setInt(1, a.getIdCompte());
			psDelete.executeUpdate ();
			rs = true;
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return rs;
	}

	@Override
	public boolean exist(String nom, String prenom) {
		try {
			psExistNom.setString(1, nom);
			psExistNom.setString(2, prenom);
			ResultSet rs = psExistNom.executeQuery ();
			return rs.getInt("exist")==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return false;
	}

	@Override
	public boolean exist(String login) {
		try {
			psExistLogin.setString(1, login);
			ResultSet rs = psExistLogin.executeQuery ();
			return rs.getInt("exist")==1;
		} catch (SQLException e) {
			e.printStackTrace();
		}		
		return false;
	}

	@Override
	public void inviterProf(Professeur p) {
		try {
			psInvitProf.setString(1, p.getNom());
			psInvitProf.setString(2, p.getPrenom());
			psInvitProf.setString(3, p.getNumProf());
			psInvitProf.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void inviterAdmin(Administrateur a) {
		try {
			psInvitAdmin.setString(1, a.getNom());
			psInvitAdmin.setString(2, a.getPrenom());
			psInvitAdmin.setString(3, a.getNumAdmin());
			psInvitAdmin.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		}		
	}

}
